package com.xindaoapp.happypet.viewpager;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.xindaoapp.happypet.R;


/**
 * ViewPager 每个窗口
 * 
 * @author chenghao
 */
public abstract class ViewPagerWindow {
    private View view;
    protected Activity mContext;
    private boolean isInited;
    private boolean isIniting;
    private boolean isEachTimeToRefursh;
    private View loading;
    private AnimationDrawable animation;
    private ImageView imageView;

    public ViewPagerWindow(Activity context) {
        super();
        this.mContext = context;
    }


    /**
     * @param context
     * @param isEachTimeToRefursh 每次切换tab都会刷新界面
     */
    public ViewPagerWindow(Activity context, boolean isEachTimeToRefursh) {
        super();
        this.mContext = context;
        this.isEachTimeToRefursh = isEachTimeToRefursh;
    }

    protected final synchronized View getView() {
        if (view == null) {
            view = onCreateView();
            initViews();
        }
        return view;
    }

    protected void initViews() {
        loading = view.findViewById(R.id.loading);
        imageView = (ImageView) view.findViewById(R.id.imageView);
        imageView.setBackgroundResource(R.anim.alertdialog_animation);
        animation = (AnimationDrawable) imageView.getBackground();
        animation.setOneShot(false);
        animation.start();
    }

    protected abstract View onCreateView();

    public final void loadData() {
        getView();
        if ((!isInited && !isIniting) || (isInited && isEachTimeToRefursh)) {
            setLoading();
            isIniting = true;
            onLoadDatas();
        }
    }

    private void setLoading() {
        View retry = view.findViewById(R.id.retry);
        if (null != loading) {
            animation.start();
            loading.setVisibility(View.VISIBLE);
        }
        if (null != retry) {
            retry.setVisibility(View.GONE);
        }
    }

    /** 回调刷新数据 */
    protected void onLoadDatas() {}

    protected void onDataArrived(boolean result) {
        if (result) {
            isInited = true;
            isIniting = false;
            setLoadingViewState(true);
        } else {
            isInited = false;
            isIniting = false;
            setLoadingViewState(false);
        }
    }

    /** 请求成功，但是没有数据结果 */
    protected void onNoDataArrived() {
        View noData = view.findViewById(R.id.no_data);
        if (null != noData) {
            noData.setVisibility(View.VISIBLE);
        }
    }

    private void setLoadingViewState(boolean result) {

        View retry = view.findViewById(R.id.retry);
        if (result) {
            if (null != loading) {
                animation.stop();
                loading.setVisibility(View.GONE);
            }
            if (null != retry) {
                retry.setVisibility(View.GONE);
            }
        } else {
            if (null != loading) {
                animation.stop();
                loading.setVisibility(View.GONE);
            }
            if (null != retry) {
                retry.setVisibility(View.VISIBLE);
            }
            if (null != retry)
                retry.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        loadData();
                    }
                });
        }
    }

    protected final Context getBaseContext() {
        return mContext;
    }
}
